package com.material.demo.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.material.demo.R;
import com.material.demo.record.AudioRecordHelper;
import com.material.demo.utils.PermissionUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by pc on 2017/2/17.
 */

public class AudioRecordActivity extends AppCompatActivity implements View.OnClickListener {

    private PermissionUtils utils;
    private AudioRecordHelper mHelper;
    private File file;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        mHelper = AudioRecordHelper.getInstance();

        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
        findViewById(R.id.btn_play).setOnClickListener(this);

     utils = PermissionUtils.createInstance(this);
        utils
                .request(0,null,Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.INTERNET);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:

                file = new File(Environment.getExternalStorageDirectory() + "/Single/" + System.currentTimeMillis() + ".pcm");
                Log.e("AudioHelper","-"+file.getPath());
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("AudioHelper","-"+file.getPath());
                }

                Log.e("AudioHelper","-"+file.exists());
                mHelper.setOutputFile(file);
                mHelper.startRecord();
                break;

            case R.id.btn_stop:

                mHelper.stopRecord();
                break;

            case R.id.btn_play:

//                try {
//                    InputStream in = getResources().getAssets().open("p1.pcm");
//                    mHelper.play(in);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }

                Log.e("AudioHelper","-"+file.getPath() +"   "+ file.length());

                if(file!=null) {
                    mHelper.play(file);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        utils.onRequestPermissionsResult(requestCode,permissions,grantResults);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.release();
    }
}
